package com.xydtech.ecard.busi.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportPerson;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportPersonService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片业务填报个人类Controller
 *
 * @author yuzhiguo
 * @date 2024-09-25 下午1:31
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务填报个人类接口")
@RequestMapping("/ecardSaasApi/busi/reportPerson")
public class EcardApiBusiReportPersonController extends EcardApiBaseController {

    @Autowired
    private IEcardApiBusiReportPersonService ecardBusiReportPersonService;
    @Autowired
    private IEcardApiBusiReportConfigService ecardBusiReportConfigService;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 电子名片-保存业务填报个人类数据接口
     *
     * @author yuzhiguo
     * @date 2024/9/25 下午2:11
     */
    @PostMapping(value = "/addReportPersonInfo")
    @ApiOperation(value = "电子名片-保存业务填报个人类数据接口", notes = "电子名片-保存业务填报个人类数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addReportPersonInfo(@RequestBody EcardApiBusiReportPerson ecardBusiReportPerson) {
        try {
            String salt = getSalt();//盐值
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ecardBusiReportPerson.getDeptId());
            //根据业务类型id查询业务类型信息
            String configId = ecardBusiReportPerson.getConfigId();
            EcardApiBusiReportConfig reportConfig = ecardBusiReportConfigService.getReportConfigById(Long.valueOf(configId));
            if ("F".equals(reportConfig.getIsMore())){//是否可以多次保存，否的话根据身份证和业务类型id校验是否已存在
                EcardApiBusiReportPerson reportPerson = new EcardApiBusiReportPerson();
                if (!StringUtils.objIsNullOrEmpty(reportPerson.getPerCustIdcard())) {
                    reportPerson.setPerCustIdcard(AESEncryptionUtils.encrypt(reportPerson.getPerCustIdcard(), salt));
                }
                reportPerson.setDeptId(dept.getDeptId());
                reportPerson.setConfigId(configId);
                List<EcardApiBusiReportPerson> busiReportPersonList = ecardBusiReportPersonService.getMyReportPersonList(reportPerson, salt);
                if (busiReportPersonList.size() > 0){
                    return ResponseModel.error("该客户已存在同类型业务！");
                }
            }
            //管户人id
            ecardBusiReportPerson.setManagementPersonnelId(ecardBusiReportPerson.getCreatorId());
            int id = ecardBusiReportPersonService.addReportPersonInfo(ecardBusiReportPerson, salt);
            return id > 0 ? ResponseModel.ok(ecardBusiReportPerson.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询个人类业务列表
     *
     * @author yuzhiguo
     * @date 2024/9/25 下午3:42
     */
    @PostMapping(value = "/getMyReportPersonList")
    @ApiOperation(value = "电子名片-条件查询个人类业务列表", notes = "电子名片-条件查询个人类业务列表")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyReportPersonList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String perCustIdcard = jsonObject.getString("perCustIdcard");//身份证号
            EcardApiBusiReportPerson ecardBusiReportPerson = new EcardApiBusiReportPerson();
            ecardBusiReportPerson.setPerCustIdcard(perCustIdcard);
            ecardBusiReportPerson.setManagementPersonnelId(Long.valueOf(currentUserId));
            List<EcardApiBusiReportPerson> busiReportPersonList = ecardBusiReportPersonService.getMyReportPersonList(ecardBusiReportPerson, salt);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReportPersonList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
